package sample.business.frame.config;

import com.google.common.collect.Maps;
import sample.DataHolder;
import sample.business.frame.Constants;
import sample.business.frame.DirVo;
import sample.business.util.FreemarkerUtil;
import sample.data.PathInfo;

import java.io.File;
import java.util.Map;

/**
 * Created by lilongfei on 2017/1/19.
 */
public class SimpleConfig implements IConfig {

    private SimpleConfig(){}

    public static SimpleConfig build(){
        return new SimpleConfig();
    }

    private DirVo dirVo;

    @Override
    public void createWebConfig() throws Exception {
        Map<String,Object> map = Maps.newHashMap();
        FreemarkerUtil.createXml("web.ftl",map,dirVo.getWebInfo()
                + File.separator + Constants.WEB_XML);
    }

    @Override
    public void createAppConfig() throws Exception {
        Map<String,Object> map = Maps.newHashMap();
        map.put("pack",dirVo.getWorkName());
        map.put("db", DataHolder.getData("db"));
        FreemarkerUtil.createXml("springmvc-config.ftl",map,dirVo.getWebInfo() +
                File.separator + Constants.WEB_MVC_XML);
        FreemarkerUtil.createXml("spring-core.ftl",map,dirVo.getContextDir()
                + File.separator + Constants.SPRING_CORE);
        FreemarkerUtil.createXml("jdbc.ftl",map,dirVo.getContextDir()
                + File.separator + Constants.JDBC_PROP);
    }

    public SimpleConfig setDirVo(PathInfo pathInfo) {
        this.dirVo = new DirVo(pathInfo);
        return this;
    }
}
